package com.yy.mydota;

import java.util.ArrayList;

import com.yy.adapter.MyFragmentPageAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
		android.widget.CompoundButton.OnCheckedChangeListener {
	private RadioGroup rg_bottom_select;
	private RadioButton rb_info_bottom, rb_resource_bottom, rb_baohe_bottom,
			rb_video_bottom;
	private Fragment information, resource, baohe, video;
	private ViewPager vp_content_main;

	private TextView txv_title;
	private LinearLayout ll_title_bar_resource;
	private ImageView iv_setting;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_main);

		initUI();
		initView();

	}

	private void initUI() {
		rb_info_bottom = (RadioButton) findViewById(R.id.rb_info_bottom);
		rb_resource_bottom = (RadioButton) findViewById(R.id.rb_resource_bottom);
		rb_baohe_bottom = (RadioButton) findViewById(R.id.rb_baohe_bottom);
		rb_video_bottom = (RadioButton) findViewById(R.id.rb_video_bottom);

		rb_info_bottom.setOnCheckedChangeListener(this);
		rb_resource_bottom.setOnCheckedChangeListener(this);
		rb_baohe_bottom.setOnCheckedChangeListener(this);
		rb_video_bottom.setOnCheckedChangeListener(this);

		txv_title = (TextView) findViewById(R.id.txv_title);
		ll_title_bar_resource = (LinearLayout) findViewById(R.id.ll_title_bar_resource);
		// 设置按钮
		iv_setting = (ImageView) findViewById(R.id.iv_setting);
		iv_setting.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent in = new Intent(MainActivity.this, SettingActivity.class);
				startActivity(in);
			}
		});
	}

	private void initView() {
		information = new InformationFragment();
		resource = new DatasFragment();
		baohe = new BaoheFragment();
		video = new VideoFragment();

		ArrayList<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(information);
		fragments.add(resource);
		fragments.add(baohe);
		fragments.add(video);
		FragmentPagerAdapter fraAdapter = new MyFragmentPageAdapter(
				getSupportFragmentManager(), fragments);
		vp_content_main = (ViewPager) findViewById(R.id.vp_content_main);
		vp_content_main.setAdapter(fraAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.rb_info_bottom:
			if (isChecked) {
				rb_info_bottom
						.setBackgroundResource(R.drawable.bottomnav_bottoms);
				rb_info_bottom.setTextColor(Color.RED);
				txv_title.setVisibility(View.VISIBLE);
				txv_title.setText("全部");
				vp_content_main.setCurrentItem(0);
			} else {
				rb_info_bottom
						.setBackgroundResource(R.drawable.bottomnav_bottomu);
				rb_info_bottom.setTextColor(Color.GRAY);
			}
			break;
		case R.id.rb_resource_bottom:
			if (isChecked) {
				rb_resource_bottom
						.setBackgroundResource(R.drawable.bottomnav_bottoms);
				rb_resource_bottom.setTextColor(Color.RED);
				txv_title.setVisibility(View.GONE);
				ll_title_bar_resource.setVisibility(View.VISIBLE);
				vp_content_main.setCurrentItem(1);
			} else {
				rb_resource_bottom
						.setBackgroundResource(R.drawable.bottomnav_bottomu);
				rb_resource_bottom.setTextColor(Color.GRAY);
				ll_title_bar_resource.setVisibility(View.GONE);
			}
			break;
		case R.id.rb_baohe_bottom:
			if (isChecked) {
				rb_baohe_bottom
						.setBackgroundResource(R.drawable.bottomnav_bottoms);
				rb_baohe_bottom.setTextColor(Color.RED);
				txv_title.setVisibility(View.VISIBLE);
				txv_title.setText("宝盒首页");
				vp_content_main.setCurrentItem(2);
			} else {
				rb_baohe_bottom
						.setBackgroundResource(R.drawable.bottomnav_bottomu);
				rb_baohe_bottom.setTextColor(Color.GRAY);
			}
			break;
		case R.id.rb_video_bottom:
			if (isChecked) {
				rb_video_bottom
						.setBackgroundResource(R.drawable.bottomnav_bottoms);
				rb_video_bottom.setTextColor(Color.RED);
				txv_title.setVisibility(View.VISIBLE);
				txv_title.setText("最新视频");
				vp_content_main.setCurrentItem(3);
			} else {
				rb_video_bottom
						.setBackgroundResource(R.drawable.bottomnav_bottomu);
				rb_video_bottom.setTextColor(Color.GRAY);
			}
			break;
		default:
			break;
		}

	}

}
